//============================================================================
// Name        : RSA.cpp
// Author      : Azadeh Nematzadeh
// Version     :
// Copyright   :
// Description : RSA
//============================================================================

#include "RSA.h"
using namespace Botan;
using namespace std;
/*
 * This is a constructor for this class
 */
RSA::RSA() {
	//init = Botan::LibraryInitializer();
}
/*
 * This function loads public from rsapub.pem file to variable publicKey.
 */
void RSA::loadPublicKey(std::string path){
	X509_PublicKey* key=X509::load_key(path);
    publicKey = dynamic_cast<RSA_PublicKey*>(key);
}
/*
 * This function loads private key from file to variable privateKey.
 */

void RSA::loadPrivateKey(std::string path,std::string passphrase){
	PKCS8_PrivateKey* key=PKCS8::load_key(path, passphrase);
    privateKey = dynamic_cast<RSA_PrivateKey*>(key);
}
/*
 * This function encrypts an array of string with public key.
 */
void RSA::encrypt(byte * vect_message, int size ,PK_Encrypting_Key* key,SecureVector<byte> *ciphertext){
	     PK_Encryptor* enc = get_pk_encryptor(*key, "EME1(SHA-1)");

		// will also accept a SecureVector<byte> as input
		*ciphertext = enc->encrypt(vect_message, size);

}
/*
 * This function decrypts an array of string with private key.
 */

void RSA::decrypt(SecureVector<byte> vect_message ,PK_Decrypting_Key* key,SecureVector<byte> *plaintext){
	 PK_Decryptor* dec = get_pk_decryptor(*key, "EME1(SHA-1)");
		try {
		// will also accept a SecureVector<byte> as input
			*plaintext = dec->decrypt(vect_message);
		}
		catch(Decoding_Error)
		{
		/* the ciphertext was invalid */
		}
}
